package com.gking.centerSystem.service.AccessIpList;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gking.centerSystem.common.BaseContext;
import com.gking.centerSystem.common.Result;
import com.gking.centerSystem.entity.AccessIpList;
import com.gking.centerSystem.mapper.AccessIpListMapper;
import com.gking.centerSystem.utils.AccessPolicyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;


@Slf4j
@Service
public class AccessIpListServiceImpl extends ServiceImpl<AccessIpListMapper, AccessIpList> implements AccessIpListService {


    /**
     * 添加受控 ip 项
     *
     * @param accessIpList 实体类
     * @return s
     */
    @Override
    public Result<String> addAccessIpList(AccessIpList accessIpList) {
        LambdaQueryWrapper<AccessIpList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccessIpList::getIpAddress, accessIpList.getIpAddress());
        AccessIpList one = this.getOne(queryWrapper);
        if (one != null) return Result.error("该ip地址已存在");

        try {
            this.save(accessIpList);
            if (Objects.equals(accessIpList.getType(), "White")) AccessPolicyUtil.resetRedisData("White");
            if (Objects.equals(accessIpList.getType(), "Black")) AccessPolicyUtil.resetRedisData("Black");

            return Result.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加失败");
        }
    }

    /**
     * 批量删除受控 ip 项
     *
     * @param ids id列表
     * @return s
     */
    @Override
    @Transactional
    public Result<String> deleteAccessIpList(List<String> ids) {
        LambdaUpdateWrapper<AccessIpList> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AccessIpList::getIsDelete, 0);
        updateWrapper.set(AccessIpList::getIsDelete, 1);
        updateWrapper.set(AccessIpList::getDeleteTime, LocalDateTime.now());
        updateWrapper.set(AccessIpList::getDeleteUserId, BaseContext.getCurrentId());
        updateWrapper.in(AccessIpList::getId, ids);

        try {
            this.update(updateWrapper);
            return Result.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }
    }

    /**
     * 批量设置受控 ip 项状态
     *
     * @param ids    id 列表
     * @param enable 启用状态
     * @return s
     */
    @Override
    public Result<String> setIpsEnableStatus(List<String> ids, Integer enable) {
        LambdaUpdateWrapper<AccessIpList> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AccessIpList::getIsDelete, 0);
        updateWrapper.set(AccessIpList::getEnable, enable);
        updateWrapper.set(AccessIpList::getUpdateTime, LocalDateTime.now());
        updateWrapper.set(AccessIpList::getUpdateUserId, BaseContext.getCurrentId());
        updateWrapper.in(AccessIpList::getId, ids);

        try {
            this.update(updateWrapper);
            return Result.success("更新状态成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新状态失败");
        }
    }

    /**
     * 更新某个受控 ip 项信息
     *
     * @param accessIpList 实体类
     * @return s
     */
    @Override
    public Result<String> updateAccessIpListItem(AccessIpList accessIpList) {
        LambdaQueryWrapper<AccessIpList> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AccessIpList::getIpAddress, accessIpList.getIpAddress());
        wrapper.eq(AccessIpList::getIsDelete, 0);
        AccessIpList one = this.getOne(wrapper);
        if (one != null && !Objects.equals(one.getId(), accessIpList.getId())) return Result.error("该IP地址已存在");

        LambdaUpdateWrapper<AccessIpList> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AccessIpList::getId, accessIpList.getId());
        updateWrapper.eq(AccessIpList::getIsDelete, 0);
        updateWrapper.set(AccessIpList::getIpAddress, accessIpList.getIpAddress());
        updateWrapper.set(AccessIpList::getType, accessIpList.getType());
        updateWrapper.set(AccessIpList::getRemark, accessIpList.getRemark());
        updateWrapper.set(AccessIpList::getUpdateTime, LocalDateTime.now());
        updateWrapper.set(AccessIpList::getUpdateUserId, BaseContext.getCurrentId());

        try {
            this.update(updateWrapper);
            if (Objects.equals(accessIpList.getType(), "White")) AccessPolicyUtil.resetRedisData("White");
            if (Objects.equals(accessIpList.getType(), "Black")) AccessPolicyUtil.resetRedisData("Black");

            return Result.success("更新信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新信息失败");
        }
    }

    /**
     * 条件分页获取受控 ip 列表
     *
     * @param pageNum      分页页码
     * @param pageSize     分页大小
     * @param accessIpList 查询条件
     * @return l
     */
    @Override
    public Result<Page> getAccessIpList(Integer pageNum, Integer pageSize, AccessIpList accessIpList) {
        LambdaQueryWrapper<AccessIpList> queryWrapper = new LambdaQueryWrapper<>();
        if (accessIpList.getIpAddress() != null)
            queryWrapper.like(AccessIpList::getIpAddress, accessIpList.getIpAddress());
        if (accessIpList.getType() != null)
            queryWrapper.eq(AccessIpList::getType, accessIpList.getType());
        if (accessIpList.getEnable() != null)
            queryWrapper.eq(AccessIpList::getEnable, accessIpList.getEnable());
        queryWrapper.eq(AccessIpList::getIsDelete, 0);
        queryWrapper.orderByDesc(AccessIpList::getCreateTime);

        try {
            Page<AccessIpList> pageInfo = new Page<>(pageNum, pageSize);
            Page<AccessIpList> pageData = this.page(pageInfo, queryWrapper);
            return Result.success("获取列表成功", pageData);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取列表失败");
        }
    }


    /**
     * 获取某个类型的所有 ip
     *
     * @param type 类型
     * @return l
     */
    @Override
    public Result<List<AccessIpList>> getAllIpByType(String type) {
        LambdaQueryWrapper<AccessIpList> queryWrapper = new LambdaQueryWrapper<>();
        if (!type.isEmpty()) queryWrapper.eq(AccessIpList::getType, type);
        queryWrapper.eq(AccessIpList::getEnable, 0);
        queryWrapper.eq(AccessIpList::getIsDelete, 0);

        try {
            List<AccessIpList> list = this.list(queryWrapper);
            return Result.success("获取列表成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取列表失败");
        }
    }
}
